<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        box {
            height: 200px;
            /* overflow: hidden; */
            position: relative;
        }

        .q {
            float: left;
            width: 200px;
            height: 200px;
            transition: 1s;
            position: absolute;
            opacity: 1;
            transition: 0.5s;
        }

        img {
            float: left;
            width: 200px;
            height: 200px;
            transition: 0.5s;
        }

        .btn {
            float: left;
            position: absolute;
            top: 200px;
            transition: 0.5s;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="q"><img src="./img/1.jpg" alt=""></div>
    </div>
    <button class="btn">切换下一张</button>
    <script>
        var arr = ["./img/1.jpg", "./img/2.jpg", "./img/3.jpg", "./img/4.jpg"]
        var num = 0
        var btn = document.querySelector(".btn")
        var img = document.querySelector("img")
        console.log(img)
        btn.onclick = function () {
            num++
            img.src = arr[num]
            img.style.opacity = 0
            if (num == 4) {
                num = 0
                img.src = arr[num]
                img.style.opacity = 1
            }
        }
        setInterval(function () {
            num++
            if (num == 4) {
                num = 0
                img.src = arr[num]
                img.style.transition = "0.5s"
                img.style.opacity = 0
            }
            img.src = arr[num]
            img.style.opacity = 1
        }, 1000)
    </script>
</body>

</html>